package controllers;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.mail.HtmlEmail;

import models.Key;
import models.LendCard;
import models.MemType;
import models.Member;
import play.Play;
import play.mvc.Controller;
import util.Util;

public class MemberRegAction extends Controller {
	public static void mregone(){
		render("/MemberReg/member1.html");
	}
	
	public static void mregtwo(){
		render("/MemberReg/member2.html");
	}
	
	public static void mregthree(){
		
		render("/MemberReg/mregthree.html");
	}
	
	public static void bgaddmember(){
		render("/MemberReg/bgaddmember.html");
	}
	
	public static void bgaddUmember(){
		render("/MemberReg/bgaddUmember.html");
	}
	
	public static void forgetpwd(){
		render("/MemberReg/forgetpwd.html");
	}
	
	public static void forgetpwd2(){
		render("/MemberReg/forgetpwd2.html");
	}
	
	public static void forgetpwd3(){
		render("/MemberReg/forgetpwd3.html");
	}
	
	public static void checkmemnum(String memnum){
		int state = 0 ;
		try{
			Member meb = Member.find("from Member where memnum = ?", memnum).first();
			if(meb != null){
				state = 1 ;
				render(state);
			}
			render(state);
		}catch(Exception e ){
			state = 0 ;
			e.printStackTrace();
			render(state);
		}
		
	}
	
	public static void checkemail(String regemail){
		int state = 0;
		try{
			Member meb = Member.find("from Member where mememail = ?", regemail).first();
			if(meb != null){
				state = 1;
			}
			render(state);
		}catch(Exception e ){
			render(state);
			e.printStackTrace();
		}
	}
	
	public static void mregsave(String uemail,String upwd,String memnum,String title,String content){
		int state = 0;
		try{
			SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyy年MM月dd日 hh:mm:ss");   
			Date   curDate   =   new   Date(System.currentTimeMillis());//获取当前时间   
			String   starttime   =   formatter.format(curDate);
			Member member = new Member();
			member.mememail = uemail ;
			member.mempass =  Util.md5(upwd) ;
			member.memnum = memnum ;
			member.memtime = starttime ;
			MemType mtype = MemType.find("from MemType where metname = ?", "普通会员").first();
			member.metid = "" + mtype.id ;
			String num = Util.uuid();
			member.lecnum = num ;
			member.save();
			LendCard lendcard=new LendCard();
			lendcard.lecnum = num ;
			lendcard.lecpass = upwd;
			lendcard.lecmone = 0.00d;
			lendcard.lecsta = 1 ;
			lendcard.save();
			//send email
			HtmlEmail email = new HtmlEmail();
			email.setCharset("utf-8");
			email.setTLS(true);
			email.setHostName("smtp.qq.com");
			email.setFrom("1650495908@qq.com", "蓝莲花有限公司");
			email.setAuthentication("1650495908@qq.com", "123456789");	
			email.addTo(uemail);
			
			title = "注册激活";
			
			content="<h2>欢迎加入蓝莲花</h2><p>祝你读书快乐！</p>";	
			
			System.out.println(content);
			email.setSubject(title.trim());
			email.setHtmlMsg(content.trim());
			// 发送邮件
			email.send();
			
			state = 1;
			render(state);
		}catch(Exception e){
			state = 0;
			render(state);
			e.printStackTrace();
		}
		
	}
	
	public static void Umregsave(String uemail,String upwd,String school,String department,String major,Integer sex,String memnum,String title,String content){
		int state = 0;
		try{
			SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyy年MM月dd日 hh:mm:ss");   
			Date   curDate   =   new   Date(System.currentTimeMillis());//获取当前时间   
			String   starttime   =   formatter.format(curDate);
			Member member = new Member();
			member.mememail = uemail ;
			member.mempass = Util.md5(upwd) ;
			member.memnum = memnum ;
			member.memtime = starttime ;
			MemType mtype = MemType.find("from MemType where metname = ?", "大学生").first(); 
			member.metid = "" + mtype.id ;
			member.school =school;
			member.department =department ;
			member.major = major ;
			member.sex = sex ;
			String num = Util.uuid();
			Member mm =Member.find("from Member where lecnum = ?", num).first();
			if(mm == null){
				member.lecnum = num;
			}else{
				num = Util.uuid();
				member.lecnum = num;
			}
			
			member.save();
			
			LendCard lendcard=new LendCard();
			lendcard.lecnum = num;
			lendcard.lecpass = upwd;
			lendcard.lecmone = 0.00d;
			lendcard.lecsta = 1 ;
			lendcard.save();
			
			//send email
			HtmlEmail email = new HtmlEmail();
			email.setCharset("utf-8");
			email.setTLS(true);
			email.setHostName("smtp.qq.com");
			email.setFrom("1650495908@qq.com", "蓝莲花有限公司");
			email.setAuthentication("1650495908@qq.com", "123456789");	
			email.addTo(uemail);
			
			title = "注册激活";
			
			content="<h2>welcome,too！</h2>";	
			
			System.out.println(content);
			email.setSubject(title.trim());
			email.setHtmlMsg(content.trim());
			// 发送邮件
			email.send();
			
			state = 1;
			render(state);
		}catch(Exception e){
			state = 0;
			render(state);
			e.printStackTrace();
		}
		
	}
	
	public static void findbackpwd(String uemail){
		int state = 0 ;
		try{
			Member member = Member.find("from Member where mememail = ?", uemail).first();
			Key keys=new Key();
			keys.key = Util.uuid();
			keys.value = member.mememail;
			keys.save();
			//send email
			HtmlEmail email = new HtmlEmail();
			email.setCharset("utf-8");
			email.setTLS(true);
			email.setHostName("smtp.gmail.com");
			email.setFrom("wangyiyan8888@gmail.com", "蓝莲花有限公司");
			email.setAuthentication("wangyiyan8888@gmail.com", "wangyiyan8877");	
			email.addTo(uemail);
			
			String title = "找回密码";
			
			System.out.print(request.host+"--------------------------------------------------------------------------");
			
			String content="<a href='http://"+request.host+"/controllers/MemberRegAction.forgettwo?key ="+keys.key+"&value="+keys.value+"'>http://"+request.host+"/controllers/MemberRegAction.forgettwo?key ="+keys.key+"&value="+keys.value+"</a>";	
			System.out.println(content);
			email.setSubject(title.trim());
			email.setHtmlMsg(content.trim());
			// 发送邮件
			email.send();
			state = 1 ;
			render(state);
		}catch(Exception e){
			state = 0 ;
			e.printStackTrace();
			render(state);
		}
	}
	
	public static void forgettwo(String key ,String value){
		Key keys = Key.find("from Key where key = ? and value = ?", key,value).first();
		if(keys != null){
			render("");
		}else{
			render("");
		}
	}
}
